package com.ZMPrinter.Demo;

import com.ZMPrinter.LSF.LSFDecoder;
import com.ZMPrinter.*;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class PrintViaNet_Demo_UHF_LSF {
    public static void main(String[] args) throws InterruptedException{

        zmprinter thiszmprinter = new zmprinter();//定义打印机对象
        zmlabel thiszmlabel = new zmlabel();//定义标签对象
        List<zmlabelobject> thiszmlabelobjectlist = new ArrayList<>();//定义标签上需要打印的对象列表
        printutility zmprintoperate = new printutility();//定义打印操作对象

        int printerDpi_old=0;//lsf文件中保存的打印机分辨率

        //region 从LSF文件中读取打印机、标签及对象列表
        LSFDecoder lsfDecoder=new LSFDecoder();
        Map map=lsfDecoder.getZMLabelObjectList("C:\\Users\\Lkf\\Desktop\\75X802.lsf");

        thiszmprinter= (zmprinter)map.get("zmprinter");//恢复打印机参数
        printerDpi_old=thiszmprinter.printerdpi;//保存原来的分辨率
        thiszmlabel= (zmlabel)map.get("zmlabel");//恢复标签参数
        thiszmlabelobjectlist = (List<zmlabelobject>)map.get("zmlabelobjectlist");//恢复标签对象列表的参数

        if(thiszmlabelobjectlist.size()==0)
        {
            System.out.println("没有生成标签对象，请检查lsf文件。");
            return;
        }
        //endregion

        //region 设置打印机传输接口
        boolean isRFIDfunction=false;
        boolean isRFIDread=false;
        boolean isRFIDwrite=false;
        for (zmlabelobject thisLabelObject : thiszmlabelobjectlist)
        {
            if (thisLabelObject.objectName.startsWith("rfiduhf"))//RFID对象
            {
                isRFIDfunction=true;
                if (thisLabelObject.ReadIDOnly)
                    isRFIDread = true;
                else
                    isRFIDwrite=true;
            }
        }
        if(isRFIDread)
            thiszmprinter.printerinterface= printerstyle.RFID_NET;//打印机接口类型
        else
            thiszmprinter.printerinterface= printerstyle.NET;
        //endregion

        //region 修改打印机的属性:打印速度、黑度、IP等
        thiszmprinter.printerdpi = 300;//打印机分辨率，203dpi、300dpi或600dpi
        thiszmprinter.printSpeed=4;//打印速度，赋值范围2~8，单位是英寸/秒
        thiszmprinter.printDarkness=7;//打印黑度，赋值范围0~20，值越大打印头加热时间越长。一般使用蜡质碳带赋值7~12，树脂碳带赋值12~18
        thiszmprinter.printernetip = "192.168.8.78";//打印机的ip地址，端口号可以不设置，默认是9100

        if(printerDpi_old!=thiszmprinter.printerdpi)
        {
            //region 打印机分辨率不同，需要处理条码缩放比例
            zmprintoperate.SetLabelObjectScale(thiszmprinter,printerDpi_old,thiszmlabelobjectlist);//重新设置缩放比例
            //endregion
        }
        //endregion

        //region 循环打印标签
        for(int pnum=1;pnum<=3;pnum++) {

            System.out.println("开始打印第"+pnum+"张");

            //region 检查打印机状态
            if(!zmprintoperate.CheckNetPrinterStatus(thiszmprinter))
            {
                System.out.println("打印机状态异常，请检查。");
                return;
            }
            //endregion

            //region 读取标签TID（如果需要）
            String tagTID = "empty";//RFID标签的TID
            if(isRFIDread) {
                if(isRFIDwrite)
                    tagTID = zmprintoperate.ReadFromPrinterUHFbyNet(thiszmprinter, 0, false);//读取标签的TID，后续有写入操作
                else
                    tagTID = zmprintoperate.ReadFromPrinterUHFbyNet(thiszmprinter, 0, true);//读取标签的TID，不需要写EPC操作
                if (tagTID.isEmpty() || tagTID.equals("X"))//未读到数据
                {
                    System.out.println("未读到标签数据。");
                    zmprintoperate.PrintBlankLabelbyNet(thiszmprinter, thiszmlabel);//打印一张空白页面，以便读取下一张标签
                    Thread.sleep(3000);
                    continue;
                }

                //更新读到的数据
                if(tagTID!=null&&!tagTID.isEmpty()) {
                    for (zmlabelobject thisLabelObject : thiszmlabelobjectlist) {
                        for (int vi = 0; vi < thisLabelObject.Variables.size(); vi++) {
                            objectvariable thisVariable = thisLabelObject.Variables.get(vi);
                            if (thisVariable.type == 4||thisVariable.type == 5) {//LSF文件中有读取HF或UHF的ID对象
                                thisVariable.data=tagTID;
                                thisLabelObject.Variables.set(vi, thisVariable);//更新数据
                            }
                        }
                    }
                }
            }
            //endregion

            //region 设置对象里变量的值（如果需要）
            //注意：参数varName是lsf文件里对象变量的“子字符串共享名称”，varValue是需要设置的新值
            zmprintoperate.setVarValue(thiszmlabelobjectlist,"v1","X1D");//设置变量的子字符串共享名称为“v1”的值
            zmprintoperate.setVarValue(thiszmlabelobjectlist,"v2","2109"+String.format("%04d",pnum));//设置变量的子字符串共享名称为“v2”的值
            //endregion

            //region 预览标签（即生成标签图片）
            BufferedImage labelimage=zmprintoperate.CreateLabelImage(thiszmprinter, thiszmlabel, thiszmlabelobjectlist);//生成标签图片
            try {
                if(labelimage!=null) {
                    File imageoutputfile = new File("labelpreview0930.png");
                    ImageIO.write(labelimage, "png", imageoutputfile);//保存图片
                }
            } catch (IOException e) {
                System.out.println("保存预览图片出错，错误信息："+e.getMessage());
            }
            //endregion

            //region 将标签生成打印数据
            byte[] zmpclecommand = zmprintoperate.CreateLabelCommand(thiszmprinter, thiszmlabel, thiszmlabelobjectlist);//生成标签数据
            if (zmpclecommand == null) {
                System.out.println("生成标签数据异常为空.");
                return;
            }
            //endregion

            //region 发送数据给打印机
            zmprintoperate.PrintLabelCommandbyNet(thiszmprinter, zmpclecommand);//发送数据给打印机
            Thread.sleep(3000);//等待打印完成，根据打印速度和标签高度估算打印时间。如果网络打印，那么需要根据网络路由延迟加大等待时间
            System.out.println(pnum+"张打印完成");
            //endregion
        }
        //endregion
    }
}
